package rak.r91.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import rak.r91.data.Item;
import rak.r91.data.ItemAttribute;
import rak.r91.data.ItemType;
import rak.r91.service.ItemManager;
import rak.r91.service.SimpleItemManager;

@Controller
@RequestMapping("/item")
@SessionAttributes({"user" ,"ItemTypes", "selectedSubType","attributeList","item"})
public class ItemController {
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView itemPage(){
		ItemManager im = new SimpleItemManager();
		ModelAndView mav = new ModelAndView("item");
		mav.addObject("ItemTypes", im.getItemTypes());
		return mav;
	}
	
	@RequestMapping("/sub")
	public ModelAndView itemTreeRequest(@RequestParam("subType") int subType, @ModelAttribute("ItemTypes")List<ItemType> itemTypesList, ModelMap model){
		ItemManager im = new SimpleItemManager();
		ModelAndView mav = new ModelAndView("item", model);
		System.out.println(model.get("ItemTypes"));
		ItemType itemType = findItemType(subType, itemTypesList);
		mav.addObject("selectedSubType", itemType);
		mav.addObject("itemList", im.getItems(itemType.getItemTypeID()));
		itemType.setItemSubTypeList(im.getItemSubTypes(itemType.getItemTypeID()));
		System.out.println(itemType.getItemType());
		return mav;
	}
	
	@RequestMapping("/add")
	public ModelAndView addItemPage(@ModelAttribute("selectedSubType")ItemType itemType){
		ItemManager im = new SimpleItemManager();
		ModelAndView mav = new ModelAndView("add");
		mav.addObject("attributeList", im.getItemTypeAttributes(itemType));
		return mav;
	}
	
	@RequestMapping("/add1")
	public ModelAndView addItem(HttpServletRequest request, @ModelAttribute("attributeList")List<ItemAttribute> itemAttributeList, @ModelAttribute("selectedSubType")ItemType selectedItemType){
		//todo hunnikus input error handlingut ja muud 
		Item item = new Item();
		item.setName(request.getParameter("name"));
		item.setDescription(request.getParameter("description"));
		item.setSalePrice(Double.parseDouble(request.getParameter("sale_price")));
		item.setItemType(selectedItemType);
		for (ItemAttribute ia : itemAttributeList) {
			String input = request.getParameter(Integer.toString(ia.getItemAttributeTypeID()));
			if(ia.getDataType()==1){
				ia.setValueText(input);
			}
			if(ia.getDataType()==2){
				ia.setValueNumber(Double.parseDouble(input));
			}
		}
		ItemManager im = new SimpleItemManager();
		im.addNewItem(item, itemAttributeList);
		///
		return new ModelAndView("index");		
	}
	
	@RequestMapping("/selected")
	public ModelAndView selectedItem(@RequestParam("item") int itemID){
		ItemManager im = new SimpleItemManager();
		Item item = im.getItemAndAttributes(itemID);
		ModelAndView mav = new ModelAndView("itemV");
		mav.addObject("item", item);
		return mav;
	}
	
	@RequestMapping("/change")
	public ModelAndView changeItemPage(){
		return new ModelAndView("change");
	}
	
	
	@RequestMapping("/change1")
	public ModelAndView changeItem(HttpServletRequest request, @ModelAttribute("item")Item item){
		//topeltkood vaja ära liigutada
		item.setName(request.getParameter("name"));
		item.setDescription(request.getParameter("description"));
		item.setSalePrice(Double.parseDouble(request.getParameter("sale_price")));
		for (ItemAttribute ia : item.getAttributeList()) {
			String input = request.getParameter(Integer.toString(ia.getItemAttributeID()));
			System.out.println(input);
			if(ia.getDataType()==1){
				ia.setValueText(input);
			}
			if(ia.getDataType()==2){
				ia.setValueNumber(Double.parseDouble(input));
			}
		}
		ItemManager im = new SimpleItemManager();
		im.changeItem(item);
		return new ModelAndView("change");
	}
	
	private ItemType findItemType(int itemTypeID, List<ItemType> topItemTypeList){
		for (ItemType itemType : topItemTypeList) {
			if(itemType.getItemTypeID()==itemTypeID){
				return itemType; 
			}
			if(itemType.getItemSubTypeList()!=null){
				ItemType tempItemType = findItemType(itemTypeID, itemType.getItemSubTypeList());
				if(tempItemType!=null)return tempItemType;
			}
		}
		return null;
	}
	

	

}
